package com.bite.gateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
@Component
public class CustomGatewayFilterFactory extends AbstractGatewayFilterFactory<CustomConfig> implements Ordered {

    public CustomGatewayFilterFactory() {
        super(CustomConfig.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("configKey", "configValue");
    }

    @Override
    public GatewayFilter apply(CustomConfig config) {
        return ((exchange, chain) -> {
            log.info("Pre-GatewayFilter..., config: {}", config);
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                log.info("Post-GatewayFilter...");
            }));
        });
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
